<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温度预测系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
            margin-top: 30px;
        }
        .result-box {
            display: none;
            padding: 20px;
            margin-top: 20px;
            border-radius: 8px;
            background-color: #e9f7ef;
            border: 1px solid #c3e6cb;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .btn-predict {
            background-color: #4CAF50;
            border-color: #4CAF50;
            width: 100%;
            padding: 10px;
            font-size: 18px;
        }
        .prediction-value {
            font-size: 2.5rem;
            font-weight: bold;
            color: #28a745;
            text-align: center;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center mb-4">温度预测系统</h1>
        <p class="text-center mb-4">请输入以下天气特征，系统将预测明天的最高温度</p>

        <form id="prediction-form">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="year">年份</label>
                        <input type="number" class="form-control" id="year" name="year" min="2000" max="2100" value="2023" required>
                    </div>

                    <div class="form-group">
                        <label for="month">月份</label>
                        <input type="number" class="form-control" id="month" name="month" min="1" max="12" value="6" required>
                    </div>

                    <div class="form-group">
                        <label for="day">日期</label>
                        <input type="number" class="form-control" id="day" name="day" min="1" max="31" value="15" required>
                    </div>

                    <div class="form-group">
                        <label for="temp_2">前天最高温度 (°C)</label>
                        <input type="number" class="form-control" id="temp_2" name="temp_2" step="0.1" value="25.5" required>
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="form-group">
                        <label for="temp_1">昨天最高温度 (°C)</label>
                        <input type="number" class="form-control" id="temp_1" name="temp_1" step="0.1" value="26.8" required>
                    </div>

                    <div class="form-group">
                        <label for="average">历史平均温度 (°C)</label>
                        <input type="number" class="form-control" id="average" name="average" step="0.1" value="24.3" required>
                    </div>

                    <div class="form-group">
                        <label for="friend">朋友预测的温度 (°C)</label>
                        <input type="number" class="form-control" id="friend" name="friend" step="0.1" value="27.5" required>
                    </div>

                    <div class="form-group">
                        <label for="weekday">星期几</label>
                        <select class="form-select" id="weekday" name="weekday" required>
                            <option value="Mon">星期一</option>
                            <option value="Tue">星期二</option>
                            <option value="Wed">星期三</option>
                            <option value="Thu">星期四</option>
                            <option value="Fri">星期五</option>
                            <option value="Sat">星期六</option>
                            <option value="Sun">星期日</option>
                        </select>
                    </div>
                </div>
            </div>

            <button type="submit" class="btn btn-primary btn-predict mt-3">预测温度</button>
        </form>

        <div class="result-box" id="result-box">
            <h3 class="text-center">预测结果</h3>
            <div class="prediction-value" id="prediction-value">-- °C</div>
            <p class="text-center">根据您提供的数据，预测明天的最高温度为 <strong id="prediction-text">--</strong> °C</p>
        </div>
    </div>

    <script>
        document.getElementById('prediction-form').addEventListener('submit', function(e) {
            e.preventDefault();

            // 显示加载状态
            const predictButton = document.querySelector('.btn-predict');
            predictButton.textContent = '预测中...';
            predictButton.disabled = true;

            // 收集表单数据
            const formData = new FormData(this);
            const data = Object.fromEntries(formData.entries());

            // 发送预测请求
            fetch('/predict', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: new URLSearchParams(data).toString()
            })
            .then(response => response.json())
            .then(result => {
                if (result.status === 'success') {
                    const prediction = result.prediction;
                    document.getElementById('prediction-value').textContent = prediction + ' °C';
                    document.getElementById('prediction-text').textContent = prediction;
                    document.getElementById('result-box').style.display = 'block';
                } else {
                    alert('预测失败: ' + result.message);
                }
            })
            .catch(error => {
                alert('请求错误: ' + error.message);
            })
            .finally(() => {
                predictButton.textContent = '预测温度';
                predictButton.disabled = false;
            });
        });
    </script>
</body>
</html>